<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商品分类管理
 */
namespace app\ais\controller\shop;
use app\ais\controller\Common;
use app\ais\model\AisShopCate;
use app\ais\model\AisShop;
use app\ais\model\AisStore;

class Cate extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'宝贝专题','url'=>url("ais/shopCate/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        $condition = [];
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $view['lists'] = AisShopCate::where($condition)->order('sort desc,id desc')->paginate(20);
        return view()->assign($view);
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'                => $this->request->param('id/d',0),
                'types'             => $this->request->param('types/d',0),
                'show_type'         => $this->request->param('show_type/d',0),
                'title'             => $this->request->param('title/s'),
                'content'           => $this->request->param('content/s'),
                'picture'           => $this->request->param('picture/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Category.save');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result =  AisShopCate::edit($data);
            if($result){
                return enjson(200,'操作成功');
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            $view['info'] = AisShopCate::where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Category.sort');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result = AisShopCate::update(['sort'=>$data['sort'],'id' => $data['id']]);
            if($result){
                return enjson(200,'操作成功');
            }else{
                return enjson(0,'操作失败');
            }
        }
    }

    //删除
    public function delete(int $id){
        $goods =  AisShop::where(['category_id' => $id])->find();
        if($goods){
            return enjson(403,'删除失败,请查看是否包含商品');
        }
        $result = AisShopCate::destroy(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id]);
        if($result){
            return enjson(200,'操作成功');
        }else{
            return enjson(403,'删除失败,请查看是否包含商品');
        } 
    }
}